package wc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class GetResult {
	int charNum;
	int wordNum;
	int lineNum;
	int blankLineNum;
	int noteLineNum;
	int codeLineNum;
    PrintToFile ptf;
    String OutputFileName = "result.txt";
    public void outputFileName(String fileName) {
    	OutputFileName = fileName;
    }
	public void getFileContent(String filePath) throws IOException {// 参数是要统计的文件路径，此方法用于打开文件并从文件中读取数据
		charNum = 0;
		wordNum = 0;
		lineNum = 0;
		blankLineNum = 0;
		noteLineNum = 0;
		codeLineNum = 0;
	
	     File file = new File(filePath);
		if (file.exists() == false) {//需要统计的文件
			System.out.println("the file  to be counted don't exist!");
			System.exit(1);
		}
		InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath));// 获得文件内容
		BufferedReader br = new BufferedReader(isr);
		String s = null;
		while ((s=br.readLine())!=null) {
			
			char[] characters = null;
			boolean isBlank = false;
			boolean isNote = false;
			if (s.length()==0) {// 判断是否是空行
				blankLineNum++;
				isBlank = true;		
			}
			charNum += s.length();//
			wordNum += s.split(" ").length;
			lineNum++;		
			characters = s.toCharArray();//将读取的行转变成字符数组
			for (int i = 0; i < characters.length; i++) {
				if (characters[i] == '/' && characters[i + 1] == '/') {//判断是否是注释行
					noteLineNum++;
					isNote = true;
				} 
			}
			if(isBlank==false&&isNote==false) {
				codeLineNum++;
			}

		}
		br.close();
	}

	public void dealChoices(String choice) throws IOException {// 处理具体的参数
                String result = null;
                ptf = new PrintToFile(OutputFileName);
                if(choice.equals("c")) {
                	result = "字符数："+charNum;
                	System.out.println(result);
                	ptf.outputToFile(result);
                }else if(choice.equals("w")) {
                	result = "单词数："+wordNum;
                	System.out.println(result);
                	ptf.outputToFile(result);
                }else if(choice.equals("l")) {
                	result = "行数："+lineNum;
                	System.out.println(result);
                	ptf.outputToFile(result);
                }else if(choice.equals("a")) {
                	result = "代码行数/空格行数/注释行数："+codeLineNum+"/"+blankLineNum+"/"+noteLineNum;
                	System.out.println(result);
                	ptf.outputToFile(result);
                }
	}
	/*public void dealParameterX(String path) throws IOException {
		File file = new File(path);
		if(file.exists()==false) {
			file.createNewFile();
		}
	   InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
	   BufferedReader br = new BufferedReader(isr);
	   String str = null;
	   while((str=br.readLine())!=null) {
		   charNum += str.length();
			wordNum += str.split(" ").length;
			lineNum++;
	   }
	   System.out.println(file.getName()+":");
	   System.out.println("字符数："+charNum);
	   System.out.println("单词数："+wordNum);
	   System.out.println("行数："+lineNum);
	}*/
}
